// 商品信息相关
import { axios } from "@/plugins/axios";
import merge from "lodash/merge";

class CouponCategory {
  // 商品分类
  constructor() {
    this.id = 0;
    this.children = []; // good for treeview
    this.category_name = "";
    this.name_key = "category_name"; // for ITreeViewItem
    this.parent_id = "";
    this.update_time = new Date();
    this.create_time = new Date();
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/category";
    let params = { ...query_args, per_page: per_page, page: page };

    return axios.get(url, { params: params }).then((res) => {
      let results = [];
      res.data.results.forEach((item) => {
        const data = merge(new CouponCategory(), item);
        results.push(data);
      });
      return { total: res.data.total, pages: res.data.pages, results: results };
    });
  }

  static async getById(id) {
    const res = await CouponCategory.fetch({ id: id }, 1, 0);
    if (res.total > 0) {
      return res.results[0];
    } else {
      return null;
    }
  }

  createOrUpdate() {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/category";
    const data = Object.assign({}, this);

    if (!data.id || data.id == 0) {
      delete data.id;
    }
    return axios.post(url, data).then((res) => {
      const data = merge(new CouponCategory(), res.data.data);
      return data;
    });
  }
  delete() {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/category";
    const params = { id: this.id };
    return axios.delete(url, { params: params }).then((res) => {
      return true;
    });
  }
}

const CouponDefCodes = [
  { name: "体验版", value: "simple" },
  { name: "多元智能", value: "omni_intelligence" },
  { name: "学习风格", value: "learn_trait_type" },
  { name: "自然属性", value: "natural_property" },
  { name: "职场报告", value: "job_report" },
  { name: "全版", value: "full_report" },
]

class CouponDef {
  // 商品详情
  constructor() {
    this.id = 0;
    this.name = "";
    this.content = "";
    this.code = "";
    this.active = true;
    this.original_prize = 0.0;
    this.prize = 0.0;
    this.count = 1; // 对应多少份
    this.category_id = 0;
    this.category = new CouponCategory();

    this.image = null;
    this.header_image = null;
    this.order_index = 1; // 排序号
    this.sold_count = 0; // 已卖出
    this.extras = null; // 额外的属性
    this.update_time = new Date();
    this.create_time = new Date();
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon_def";
    let params = { ...query_args, per_page: per_page, page: page };

    return axios.get(url, { params: params }).then((res) => {
      let results = [];
      res.data.results.forEach((item) => {
        const data = merge(new CouponDef(), item);
        results.push(data);
      });
      return { total: res.data.total, pages: res.data.pages, results: results };
    });
  }

  static async getById(id) {
    const res = await CouponDef.fetch({id: id})
    return res.results[0];
  }

  async createOrUpdate() {
    const data = Object.assign({}, this);
    if(!data.category_id){
      data.category_id = data.category.id
    }
    if (!data.id || data.id == 0) {
      delete data.id;
      delete data.unique_id;
      const url = process.env.VUE_APP_API_URL + "api/v1/coupon_def";
      const res = await axios.post(url, data).then((res) => {
        const data = merge(new CouponDef(), res.data.data);
        return data;
      });
      return res;
    } else {
      // 修改
      const url = process.env.VUE_APP_API_URL + `api/v1/coupon_def?id=${data.id}`;
      const res = await axios.put(url, data).then((res) => {
        const data = merge(new CouponDef(), res.data.data);
        return data;
      });
      return res;
    }
  }
  async delete() {
    const url = process.env.VUE_APP_API_URL + `api/v1/coupon_def?id=${this.id}`;
    await axios.delete(url);
    return true;
  }
}

class LunboCoupon {
  // 轮播商品
  constructor() {
    this.id = 0;
    this.coupon_id = 0;
    this.coupon_def = new CouponDef()
    this.name = "";
    this.order_index = 1;
    this.update_time = new Date();
    this.create_time = new Date();
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/lunbo";
    let params = { ...query_args, per_page: per_page, page: page };

    return axios.get(url, { params: params }).then((res) => {
      let results = [];
      res.data.results.forEach((item) => {
        const data = merge(new LunboCoupon(), item);
        results.push(data);
      });
      return { total: res.data.total, pages: res.data.pages, results: results };
    });
  }

  static async getById(id) {
    const res = await LunboCoupon.fetch({ id: id }, 1, 0);
    if (res.total > 0) {
      return res.results[0];
    } else {
      return null;
    }
  }

  createOrUpdate() {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/lunbo";
    const data = Object.assign({}, this);

    if (!data.id || data.id == 0) {
      delete data.id;
    }
    if(data.coupon_def && data.coupon_def.id>0){
      data.coupon_id = data.coupon_def.id
      delete data.coupon_def
    }
    return axios.post(url, data).then((res) => {
      const data = merge(new LunboCoupon(), res.data.data);
      return data;
    });
  }
  delete() {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/lunbo";
    const params = { id: this.id };
    return axios.delete(url, { params: params }).then((res) => {
      return true;
    });
  }
}

class BaokuanCoupon {
  // 爆款商品
  constructor() {
    this.id = 0;
    this.coupon_id = 0;
    this.coupon_def = new CouponDef()
    this.name = "";
    this.order_index = 1;
    this.update_time = new Date();
    this.create_time = new Date();
  }

  static fetch(query_args = {}, page = 1, per_page = 10) {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/baokuan";
    let params = { ...query_args, per_page: per_page, page: page };

    return axios.get(url, { params: params }).then((res) => {
      let results = [];
      res.data.results.forEach((item) => {
        const data = merge(new BaokuanCoupon(), item);
        results.push(data);
      });
      return { total: res.data.total, pages: res.data.pages, results: results };
    });
  }

  static async getById(id) {
    const res = await BaokuanCoupon.fetch({ id: id }, 1, 0);
    if (res.total > 0) {
      return res.results[0];
    } else {
      return null;
    }
  }

  createOrUpdate() {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/baokuan";
    const data = Object.assign({}, this);

    if (!data.id || data.id == 0) {
      delete data.id;
    }
    if(data.coupon_def && data.coupon_def.id>0){
      data.coupon_id = data.coupon_def.id
      delete data.coupon_def
    }
    return axios.post(url, data).then((res) => {
      const data = merge(new BaokuanCoupon(), res.data.data);
      return data;
    });
  }
  delete() {
    const url = process.env.VUE_APP_API_URL + "api/v1/coupon/baokuan";
    const params = { id: this.id };
    return axios.delete(url, { params: params }).then((res) => {
      return true;
    });
  }
}


export { CouponCategory, CouponDefCodes, CouponDef, LunboCoupon, BaokuanCoupon };
